<template>
	<view class="jobList">
		<view class="button">
			<u-button @tap="selectCity()" type="primary"
				:custom-style="{border:'none',background:'rgba(255,255,255,0)',color:'#FFC60F'}" :hair-line='false'>
				<u-icon name="map" color="FFC60F"></u-icon>
				{{city.cityName}}
			</u-button>
		</view>
		

		<view v-show="authority==1">
			<view v-if="jobList.length==0">
				<u-empty ></u-empty>
			</view>
			<JobCard @goJobDetail="goJobDetail(item)" v-for="(item,index) in jobList" :key="index" :jobInfo="item" />
		</view>
		<view v-show="authority==2" class="jobList">
			<candidatesCard @goCandidateDetail="goCandidateDetail(item)" v-for="(item,index) in candidatesList" :candidates="item"></candidatesCard>
		</view>
	</view>
</template>

<script>
	import candidatesCard from '../../components/candidatesCard.vue'
	import {
		saveUserLog
	} from '@/api/index.js'
	import JobCard from '../../components/jobCard.vue'
	// import { jobList } from '../../json/jobList.js'
	import {
		Toast,
		setTabBarCity
	} from '../../common/common.js'
	import {
		ApiGetJobList,
		conditionCandidate
	} from '../../api/index.js'
	export default {
		components: {
			JobCard,
			candidatesCard
		},
		data() {
			return {
				jobList: [],
				jobPage: {
					current: 1,
					total: 0,
					size: 10
				},
				pageVo: {
					"salary": "",
					"cityId": "",
					"occupationId": ""
				},
				city: {

				},
				authority:1,
				cityId:null,
				candidatesPage:{
					current:1,
					size:10
				},
				candidatesVo:{
					current:1,
					size:10
				},
				candidatesList:[]
			}
		},
		onLoad() {
			this.authority = getApp().globalData.authority
			
			this.pageVo.cityId = getApp().globalData.city.cityId
			this.cityId = getApp().globalData.city.cityId
			this.getJobList()
			this.city = getApp().globalData.city
			if(this.authority==2){
				this.candidatesList = []
				this.candidatesPage = {
					current:1,
					size:10
				}
				this.conditionCandidate()
			}
		},
		onShow() {
			this.authority = getApp().globalData.authority
			
			if (uni.getStorageSync('chooseCity')) {
				setTabBarCity()
				this.city = getApp().globalData.city
				this.pageVo.cityId = getApp().globalData.city.cityId
				this.jobList = []
				this.getJobList()
				uni.removeStorageSync('chooseCity')
			}
			if(this.authority==2){
				this.candidatesList = []
				this.candidatesPage = {
					current:1,
					size:10
				}
				this.conditionCandidate()
			}
		},
		// 页面上拉触底事件
		onReachBottom() {
			if(this.authority==2){
				if(this.candidatesList.length>=this.candidatesPage.total){
					Toast({
						title: '没有更多数据了'
					})
					return
				}
				this.candidatesPage.current+=1
				this.conditionCandidate()
				return
			}
			if (this.jobList.length >= this.jobPage.total) {
				Toast({
					title: '没有更多数据了'
				})
				return
			}
			let token = uni.getStorageSync('token');
			if(token==''){
				this.jobPage.current += 1
			this.getJobList()
			}
			
		},
		methods: {
			conditionCandidate(){
				this.candidatesVo.cityId = this.cityId
				this.candidatesVo.current = this.candidatesPage.current;
				this.candidatesVo.size = this.candidatesPage.size;
				conditionCandidate(this.candidatesVo).then(res=>{
					if(res.data.code==20000){
						res.data.data.records.forEach(item => {
							this.candidatesList.push(item);
						})
						
						this.candidatesPage = res.data.data;
					}
				})
			},
			getJobList() {
				ApiGetJobList(this.jobPage, this.pageVo).then(res => {
					// console.log(this.pageVo)
					if (res.data.code == 20000) {
						res.data.data.records.forEach(item => {
							this.jobList.push(item);
						})
						this.jobPage = res.data.data;
					} else {
						Toast({
							title: res.data.message
						})
					}
				})
			},
			goJobDetail(e) {
					let token = uni.getStorageSync('token');
					let user = uni.getStorageSync('user');
					if(token!=''){
						saveUserLog({'userId':user.id,'infoId':e.id,'score':2})
					}
				uni.navigateTo({
					url: `/pageB/jobDetail/jobDetail?jobId=${e.id}`
				})
			},
			goCandidateDetail(e){
				uni.navigateTo({
					url:`../../pageB/candidatesDetail/candidatesDetail?jobId=${e.id}`
				})
			},
			selectCity() {
				uni.navigateTo({
					url: '/pageB/city/city'
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.jobList {
		background: #f4f4f4;
		min-height: 100vh;
		padding-top: 20upx;
	}

	.button {
		// margin-bottom: 20upx;
		width: 160upx;
	}

	.u-content {
		padding: 150px 60px 0;
		@include flex(column);
		align-items: center;
		justify-content: center;

		&__circle {
			background-color: $u-error;
			@include flex;
			border-radius: 100px;
			width: 60px;
			height: 60px;
			align-items: center;
			justify-content: center;
		}

		&__normal {
			font-size: 15px;
			color: $u-error;
			margin-top: 15px;
		}
	}
</style>
